package com.pf.java.File;

import java.io.*;

/**
 * 文件流
 *
 * ClassName:IO4
 * Package:com.pf.java.File
 *
 * @author: pf
 * @date: 2021/11/6-14:33
 */
public class IO4 {

    public static void main(String[] args) {
//        copyFile("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/牛客网并发.md"
//                , "C:/Users/10970/Desktop/Java学习笔记/牛客网学习/牛-副本.md" );


        copyTextFile("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/Git使用.md"
                , "C:/Users/10970/Desktop/Java学习笔记/牛客网学习/牛客-副本.md" );
    }

    // 字节流拷贝
    public static void copyFile(String srcFilePath, String destFilePath) {
        try (
                FileInputStream fileInputStream = new FileInputStream(srcFilePath);
                FileOutputStream fileOutputStream = new FileOutputStream(destFilePath);
        ) {
            byte[] bytes = new byte[128];
            int len = 0; //实际读取的字节数
            while ((len = fileInputStream.read(bytes, 0, 128)) > 0) {
                fileOutputStream.write(bytes, 0, len);
            }
            System.out.println(len);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 字符流文件拷贝
    public static void copyTextFile(String srcFilePath, String destFilePath) {
        try (
                FileReader fileReader = new FileReader(srcFilePath);
                FileWriter fileWriter = new FileWriter(destFilePath);
                ) {
            char[] chars = new char[128];
            int len = 0; //实际读取的字符数
            while ((len = fileReader.read(chars, 0, 128)) > 0) {
                System.out.print(String.valueOf(chars, 0, len));
                fileWriter.write(chars, 0, len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
